import { defineStore } from 'pinia'
import IUsers from '../interface/IUsers'
import { storage } from '../utils'
// const currentUser = storage.get('CURRENT_USER', {})
export default defineStore('base', {
  state: () => ({
    hasNotice: JSON.parse(storage.get('HAS_NOTICE', 'false')) as boolean, // 是否有新通知
    user: JSON.parse(storage.get('CURRENT_USER', '{}')) as IUsers,
    accessToken: '' as String,
    userApprovalList: JSON.parse(storage.get('USER_APPROVAL_LIST', '[]')) as any[], // 用户审批列表
    userApprovalList2: JSON.parse(storage.get('USER_APPROVAL_LIST_2', '[]')) as any[], // 用户审批列表
    userApprovalList108: JSON.parse(storage.get('USER_APPROVAL_LIST_108', '[]')) as any[], // 用户审批列表
    userApprovalList109: JSON.parse(storage.get('USER_APPROVAL_LIST_109', '[]')) as any[], // 用户审批列表
    userAllList: JSON.parse(storage.get('USER_ALL_LIST', '[]')) as any[], // 全部用户
  }),
  actions: {
    loadNotice() {
      const hasNotice = storage.get('HAS_NOTICE', 'false');
      this.$patch({ hasNotice: JSON.parse(hasNotice) });
    },
    setNotice(newState: boolean) {
      storage.set('HAS_NOTICE', JSON.stringify(newState));
      this.$patch({ hasNotice: newState });
    },
    getNotice(): boolean {
      this.loadNotice();
      return this.hasNotice as boolean;
    },
    loadState() {
      const currentUser = storage.get('CURRENT_USER', JSON.stringify({ id: 0, username: '', password: '', token: '' }));
      this.$patch({ user: JSON.parse(currentUser) });
    },
    setAllState(newState: Partial<any>) {
      // console.log('123456789')
      storage.set('CURRENT_USER', JSON.stringify({ ...newState }));
      this.$patch({ user: newState });
    },
    getAllState(): IUsers {
      this.loadState();
      return { ...this.user };
    },
    resetState() {
      this.$reset();
    },
    loadAccessToken() {
      const currentToken = storage.get('CURRENT_TOKEN', null);
      this.$patch({ accessToken: currentToken });
    },
    setAccessToken(newState: String) {
      // console.log('setAccessToken')
      storage.set('CURRENT_TOKEN', newState);
      const newStateAsString: String = String(newState);
      this.$patch({ accessToken: newStateAsString });
    },
    getAccessToken(): String {
      this.loadAccessToken();
      return this.accessToken;
    },

    // 审批列表（主管）
    loadUserApprovalList() {
      const currentUser = storage.get('USER_APPROVAL_LIST', '[]');
      this.$patch({ userApprovalList: JSON.parse(currentUser) });
    },
    setUserApprovalList(newState: Partial<any[]>) {
      storage.set('USER_APPROVAL_LIST', JSON.stringify(newState));
      this.$patch({ userApprovalList: newState });
    },
    getUserApprovalList(): any[] {
      this.loadUserApprovalList();
      return { ...this.userApprovalList };
    },
    resetUserApprovalList() {
      this.$reset();
    },
    // 审批列表（总监）
    loadUserApprovalList2() {
      const currentUser = storage.get('USER_APPROVAL_LIST_2', '[]');
      this.$patch({ userApprovalList2: JSON.parse(currentUser) });
    },
    setUserApprovalList2(newState: Partial<any[]>) {
      storage.set('USER_APPROVAL_LIST_2', JSON.stringify(newState));
      this.$patch({ userApprovalList2: newState });
    },
    getUserApprovalList2(): any[] {
      this.loadUserApprovalList2();
      return { ...this.userApprovalList2 };
    },
    resetUserApprovalList2() {
      this.$reset();
    },
     // 审批列表（副总经理）
    loadUserApprovalList108() {
      const currentUser = storage.get('USER_APPROVAL_LIST_108', '[]');
      this.$patch({ userApprovalList108: JSON.parse(currentUser) });
    },
    setUserApprovalList108(newState: Partial<any[]>) {
      storage.set('USER_APPROVAL_LIST_108', JSON.stringify(newState));
      this.$patch({ userApprovalList108: newState });
    },
    getUserApprovalList108(): any[] {
      this.loadUserApprovalList108();
      return { ...this.userApprovalList108 };
    },
    resetUserApprovalList108() {
      this.$reset();
    },
     // 审批列表（总经理）
    loadUserApprovalList109() {
      const currentUser = storage.get('USER_APPROVAL_LIST_109', '[]');
      this.$patch({ userApprovalList109: JSON.parse(currentUser) });
    },
    setUserApprovalList109(newState: Partial<any[]>) {
      storage.set('USER_APPROVAL_LIST_109', JSON.stringify(newState));
      this.$patch({ userApprovalList109: newState });
    },
    getUserApprovalList109(): any[] {
      this.loadUserApprovalList109();
      return { ...this.userApprovalList109 };
    },
    resetUserApprovalList109() {
      this.$reset();
    },
    // 全部用户列表
    loadUserAllList() {
      const currentUser = storage.get('USER_ALL_LIST', '[]');
      this.$patch({ userAllList: JSON.parse(currentUser) });
    },
    setUserAllList(newState: Partial<any[]>) {
      storage.set('USER_ALL_LIST', JSON.stringify(newState));
      this.$patch({ userAllList: newState });
    },
    getUserAllList(): any[] {
      this.loadUserAllList();
      return { ...this.userAllList };
    },
    resetUserAllList() {
      this.$reset();
    },
  }
})
